var turningAllowed = true;
var shootingAllowed = false;
var playerName = "";
var mooda = new Audio("audio/mooda.wav");
var pihta = new Audio("audio/pihtas.wav");
var pihtaspohjas = new Audio("audio/pihtaspohjas.wav");


function turnShipOnDblClick() {
	if(turningAllowed){
		$("#errorArea").hide("fast");
		var shipId = "#" + $(this).attr("id");
		var src = $(shipId).attr("src");
		var cssLeft = $(shipId).css("left");
		cssLeft = cssLeft.substr(0, cssLeft.length - 2);
		if (myBoard.getShipsOffBoard($(this).attr("id")) > -1) {
			$("#errorArea").show("fast");
        	document.getElementById("errorArea").innerHTML = "You can't maneuver at port";
        }
		else {
		if (src.indexOf("H") == -1) {
			src = src.replace("V", "H");
			$(shipId).attr("src", src);
			if (cssLeft > -20) {
				$(shipId).css("top", "+=" + (shipId.charAt(shipId.length - 3) - 1) * 10 + "px");

				if (src.substring(9, 11) % 40 == 0) {
					$(shipId).css("left", "-=" + "20px");
				}
			}
		}
		else {
			src = src.replace("H", "V");
			$(shipId).attr("src", src);
			if (cssLeft > -20) {
				$(shipId).css("top", "-=" + (shipId.charAt(shipId.length - 3) - 1) * 10 + "px");
				if (src.substring(9, 11) % 40 == 0) {
					$(shipId).css("left", "+=" + "20px");
				}
			}
		}

			myBoard.rotateShip($(this).attr("id"));
		}
	}
}

function handleDropEvent(event, ui) {
	$("#errorArea").hide("fast");
	var draggable = ui.draggable;
	var shipId = draggable.attr('id');
	var shipLength;
	var src = $("#" + shipId).attr("src");
	var positioning = (src.indexOf("H") == -1) ? "V" : "H";
	var dropId = $(this).attr("id");
	shipLength = shipId.charAt(4);
	myBoard.setShip(dropId, shipLength, positioning, shipId);
	$("#" + shipId + "1").show("fast");
}


function updateBoard(boardId, row, col, result){
	if (result == 0){
		mooda.play();
		$("#errorArea").hide("fast");
		$("#" + boardId + row.toString() + col.toString()).addClass("shootedd").removeClass("shootable");
		if (boardId == 1){
			document.getElementById("errorArea").innerHTML = "Opponent's turn";
			$("#errorArea").show("fast");
		}
		else{
			document.getElementById("errorArea").innerHTML = "Your turn";
			$("#errorArea").show("fast");
		}
	}
	else if(result == 1){
		pihta.play();
		$("#" + boardId + row.toString() + col.toString()).addClass("hit").removeClass("shootable");
		$("#" + boardId + row.toString() + col.toString() + " .explosion").show("explode","slow");
	}
	else{
		pihtaspohjas.play();
		wreck = result.split(", ");
		len = wreck[0].charAt(4);
		row = wreck[1];
		col = wreck[2];
		$("#" + boardId + row.toString() + col.toString()).addClass("hit").removeClass("shootable");
		$("#" + boardId + row.toString() + col.toString() + " .explosion").show("explode","slow");
	    if (wreck[3].toString() == "V") {
	        for (var i = 0; i < len; i++) {
	        	$("#" + boardId + (parseInt(row) + parseInt(i)).toString() + col + " .explosion").hide("fast");
	        	$("#" + boardId + (parseInt(row) + parseInt(i)).toString() + col + " .wrecked").show("explode","slow");
	        	$("#" + boardId + (parseInt(row) + parseInt(i)).toString() + col).removeClass("shootable").removeClass("hit");
	        }
	    }
	    else {
	        for (var i = 0; i < len; i++) {
	        	$("#" + boardId + row + (parseInt(col) + parseInt(i)).toString() + " .explosion").hide("fast");
	        	$("#" + boardId + row + (parseInt(col) + parseInt(i)).toString() + " .wrecked").show("explode","slow");   
	        	$("#" + boardId + row + (parseInt(col) + parseInt(i)).toString()).removeClass("shootable").removeClass("hit");
	        }
	    }
	}
}








